package org.galaxyworld.flycode.jobs;

import org.galaxyworld.flycode.config.ConfigManager;
import org.galaxyworld.flycode.config.JdbcConfigData;
import org.galaxyworld.flycode.core.AbstractGenerator;

import java.sql.*;

public abstract class AbstractDatabaseGenerator extends AbstractGenerator {

    @Override
    public void generate() {
        JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);

        try {
            showMessage("Connecting database...");
            Class.forName(jdbcConfigData.getDriver());
            Connection conn = DriverManager.getConnection(
                    jdbcConfigData.getUrl(),
                    jdbcConfigData.getUserName(),
                    jdbcConfigData.getPassword());
            showMessage("Connected done.", "Reading tables...");
            doWithConnection(conn);
            showMessage("Finished!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected abstract void doWithConnection(Connection connection) throws SQLException;

}
